#include <machine/asmdefs.h>

ASM_HEADER

FNDEFN_BEGIN(demod11mbps_asm)

	stw		%r0,%r1,-4
	stw		%r4,%r1,-8
	stw		%r5,%r1,-12
	stw		%r6,%r1,-16

/* put siAccumI,Q in r8,r3 */
        luh             %r0,%r8,0
        luh             %r5,%r9,0
        luh             %r4,%r8,2
        luh             %r6,%r9,2       ||      mulss       %r3,%r0,%r5
        stuh            %r0,%r9,0       ||      mulss       %r5,%r4,%r5
        stuh            %r4,%r9,2       ||      mulss       %r8,%r4,%r6

        add             %r8,%r3,%r8
        mulss           %r4,%r0,%r6     ||      lb          %r6,%r14,0

        lir             %r9,0

/* put abs(siAccumI) in r8 */
        cmpgt           0,%cf0,%r8,%r9
        jc              1,%cf0,SET_I    ||      subf        %r3,%r4,%r5
        subf            %r8,%r8,%r9
SET_I:
/* put abs(siAccumQ) in r3 */
        cmpgt           0,%cf1,%r3,%r9
        jc              1,%cf1,SET_Q
        subf            %r3,%r3,%r9
SET_Q:
        cmpgt           0,%cf2,%r8,%r3
        jc              1,%cf2,I_BIG

Q_BIG:
        jc              1,%cf1,END_SET_TEMP  ||      lir         %r0,2

        j               END_SET_TEMP    ||      lir         %r0,1

I_BIG:
        jc              1,%cf0,END_SET_TEMP ||      lir         %r0,0

        lir             %r0,3

END_SET_TEMP:
/* now r0 contains ucTempBit */

        /* r8 <- pucStateTable + *ucDescr */
        add             %r8,%r12,%r6
        xor             %r0,%r0,%r10    ||        lb    %r8,%r8,0
        sli             %r11,%r11,2
        or              %r0,%r11,%r0
        andil           %r9,%r0,0xF

        /* 4 bit descrambler */
        xor             %r3,%r9,%r8
        shri            %r6,%r6,4
        sli             %r9,%r9,3
        or              %r6,%r6,%r9

        /* 4 bit descrambler */
        add             %r12,%r12,%r6
        shri            %r0,%r0,4      ||       lb      %r12,%r12,0
        andil           %r0,%r0,0xF
        xor             %r12,%r0,%r12

        shri            %r6,%r6,4
        sli             %r0,%r0,3
        or              %r6,%r6,%r0     ||        lw    %r11,%r15,0
        sli             %r12,%r12,4     ||        stb   %r6,%r14,0
        or              %r8,%r12,%r3


        /* CRC */
        andil           %r9,%r11,0xFF   ||	  lw	%r0,%r1,-4
        xor             %r9,%r9,%r8     ||        lw	%r4,%r1,-8
        sli             %r9,%r9,2       ||        lw	%r5,%r1,-12
        add             %r9,%r9,%r13    ||        lw    %r6,%r1,-16
        shri            %r11,%r11,8     ||        lw    %r9,%r9,0

        xor             %r11,%r11,%r9
        stw             %r11,%r15,0	||	ji		%jt0,8

FNDEFN_END(demod11mbps_asm)

